package com.fejal.dao;


import java.util.List;

import com.fejal.bean.Horario;
import com.fejal.bean.ProfessorDisciplina;
import com.fejal.bean.Turma;
import com.fejal.bean.TurmaOferta;
import com.fejal.bean.TurmaOfertaDisciplina;


public class TurmaDAOHibernate implements TurmaDAO {

	HibernateDAOFactory hibernateDAOFactory = null;
	HibernateDAO<Turma> hibernateDAO = null;

	public TurmaDAOHibernate() {
		hibernateDAOFactory = new HibernateDAOFactory();
		hibernateDAO = hibernateDAOFactory.getTurmaDao();
	}

	@Override
	public boolean inserir(Turma turma) throws Exception {

		boolean ok = false;

		try {
			hibernateDAO.add(turma);
			ok = true;

		} catch (Exception e) {
			throw new Exception(e.getMessage());
		} 

		return ok;
	}

	@Override
	public boolean alterar(Turma turma) throws Exception {

		boolean ok = false;
	
		
		try {
			hibernateDAO.update(turma);
			ok = true;

		} catch (Exception e) {
			throw new Exception(e.getMessage());
		} 

		return ok;
	}

	@Override
	public boolean excluir(Turma turma) throws Exception {

		boolean ok = false;

		try {
			hibernateDAO.remove(turma);
			ok = true;

		} catch (Exception e) {
			throw new Exception(e.getMessage());
		} 
		return ok;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Turma> getSelecionar(String descricao) {
		String 	sql = "FROM Turma WHERE codigoTurma <> 0 ";

		if (!(descricao.isEmpty())) {
			sql += "AND descricao like '" + descricao + "%' ";  
		}
		
		sql += "ORDER BY descricao";		
 
		List<Turma> lista = hibernateDAO.getSession().createQuery(sql).list();
		return lista;
	}

	@Override
	public Turma getTurma(int codigoTurma) {
		
		Turma turma = hibernateDAO.get(codigoTurma);
		return turma;
	}

	@Override
	public List<Turma> getListTodos() {
		
		List<Turma> list = hibernateDAO.listAll();
		return list;
	}

	
	@SuppressWarnings("unchecked")
	@Override
	public boolean isExiste(Turma turma) {
		// SQL para verificar se a Turma existe
		String 	sql = 	"FROM Turma " +
				"WHERE codigoTurma = "+turma.getCodigoTurma();

		List<Turma> lista = hibernateDAO.getSession().createQuery(sql).list();
		if(lista.size()<1){
			return false;
		} else {
			return true;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Turma> getSelecionar(int codigoCurso, int codigoGradeCurricular) {
		String 	sql = "FROM Turma WHERE codigoTurma <> 0 ";

		if (codigoCurso != 0 ) {
			sql += "AND codigoCurso = '" + codigoCurso + "' ";  
		}

		if (codigoGradeCurricular != 0 ) {
			sql += "AND codigoGradeCurricular = '" + codigoGradeCurricular + "' ";  
		}
		
		sql += "ORDER BY descricao";		
 
		List<Turma> lista = hibernateDAO.getSession().createQuery(sql).list();
		return lista;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Turma> getSelecionar(String descricao, String curso, int tipoCurso) {
		String 	sql = 	" SELECT t " +
						" FROM 	Turma as t " +
						" INNER JOIN t.curso as c" +
						" WHERE codigoTurma <> 0 ";

		if (!(descricao.isEmpty())) {
			sql += "AND t.descricao like '" + descricao + "%' ";  
		}

		if (!(curso.isEmpty())) {
			sql += "AND t.curso.descricao like '" + curso + "%' ";  
		}

		if (tipoCurso != 0 ) {
			sql += "AND c.tipoCurso = "+tipoCurso+" ";  
		}

		sql += "ORDER BY t.descricao";		
 
		List<Turma> lista = hibernateDAO.getSession().createQuery(sql).list();
		return lista;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Turma> getSelecionar(String descricao, String curso) {
		String 	sql = "FROM Turma as turma WHERE turma.codigoTurma <> 0 ";

		if (!(descricao.isEmpty())) {
			sql += "AND turma.descricao like '" + descricao + "%' ";  
		}

		if (!(curso.isEmpty())) {
			sql += "AND turma.curso.descricao like '" + curso + "%' ";  
		}
		
		sql += "ORDER BY turma.descricao";		
 
		List<Turma> lista = hibernateDAO.getSession().createQuery(sql).list();
		return lista;
	}

	@SuppressWarnings("unused")
	@Override
	public void inicialize(Turma turma) {
		if (turma != null){
			Turma t = DAOFactory.getTurmaDAO().getTurma(turma.getCodigoTurma());
			for (TurmaOferta turmaOferta : t.getTurmaOferta()){
				for (TurmaOfertaDisciplina turmaOfertaDisciplina : turmaOferta.getTurmaOfertaDisciplina()){
					for (ProfessorDisciplina professorDisciplina : turmaOfertaDisciplina.getProfessorDisciplina()){
						
					}
					for (Horario horario : turmaOfertaDisciplina.getHorarios()){
						
					}
				}
			}
			turma.setTurmaOferta(t.getTurmaOferta());
			turma.setGradeCurricular(t.getGradeCurricular());
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Turma> getSelecionar(String descricao, String curso, int ano,
			int semestre) {
		String 	sql = " FROM Turma as turma " +
					  " INNER JOIN turma.turmaOferta as turmaOferta " +
					  " WHERE turma.codigoTurma <> 0 ";

		if (!(descricao.isEmpty())) {
			sql += "AND turma.descricao like '" + descricao + "%' ";  
		}

		if (!(curso.isEmpty())) {
			sql += "AND turma.curso.descricao like '" + descricao + "%' ";  
		}

		if (ano != 0) {
			sql += "AND turmaOferta.ano = " + ano ;  
		}

		if (semestre != 0) {
			sql += "AND turmaOferta.semestre = " + semestre ;  
		}

		sql += "ORDER BY turma.descricao";		
 
		List<Turma> lista = hibernateDAO.getSession().createQuery(sql).list();
		return lista;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Turma> getSelecionar(int codigoCurso) {
		String 	sql = "FROM Turma WHERE codigoTurma <> 0 ";

		if (codigoCurso != 0 ) {
			sql += "AND codigoCurso = '" + codigoCurso + "' ";  
		}
		
		sql += "ORDER BY descricao";		
 
		List<Turma> lista = hibernateDAO.getSession().createQuery(sql).list();
		return lista;
	}

}
